package cn.wuyi.common.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 对象序列化器
 */
public class ObjectSerializeUtil {
	private static	Logger log = LoggerFactory.getLogger(ObjectSerializeUtil.class);	
    /**
     * 将对象写到文件中
     * @param fileName
     * @param o
     */
	public static void serializeObjectAsFile(String fileName,Object o){
		FileOutputStream out=null;
		ObjectOutputStream  objectOut=null;
		try {
			out = new FileOutputStream(fileName);
			objectOut = new ObjectOutputStream(out);
			objectOut.writeObject(o);
			objectOut.flush();
		} catch (Exception e) {
			log.error(e.getMessage());
		}
	}
	
	/**
     * @param 将文件反序列化为对象
     * @param o
     */
	public static Object  deSerializeFileAsObject(String fileName){
		ObjectInputStream objectIn=null;
		FileInputStream in=null;
		Object result=null;
		try {
			in = new FileInputStream(fileName);
			objectIn = new ObjectInputStream(in);
			result=objectIn.readObject();
			objectIn.close();
			return  result;
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return result;
	}
}
